/*
 * Copyright 2009 SHOP.COM
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.shop.opensocial.mediated.shindig.factories;

import org.apache.shindig.protocol.model.EnumImpl;
import org.apache.shindig.protocol.model.Enum;
import java.util.ArrayList;
import java.util.List;

/**
 * Factory for converting between Mediator types and those odd wrapped Shindig types. Shindig unfortunately has a class named "Enum" which
 * collides with the JDK Enum. So, this class is a bit confusing. I've created a ticket with Shindig to fix this.
 *
 * @author Jordan Zimmerman
 */
public class WrappedEnumMapper<MEDIATED extends java.lang.Enum<MEDIATED>, SHINDIG extends java.lang.Enum<SHINDIG> & Enum.EnumKey>
{
	/**
	 * @param fromValues list of mediated enum values
	 * @param toValues list of shindig enum values
	 */
	public WrappedEnumMapper(MEDIATED[] fromValues, SHINDIG[] toValues)
	{
		fMapper = new EnumMapper<MEDIATED,SHINDIG>(fromValues, toValues);
	}

	/**
	 * Given a mediated enum value, return the shindig enum value
	 *
	 * @param m mediated value
	 * @return shindig value or null
	 */
	public Enum<SHINDIG> toShindig(MEDIATED m)
	{
		return (m != null) ? new EnumImpl<SHINDIG>(fMapper.to(m)) : null;
	}

	/**
	 * Given a shin dig enum value, return the mediated enum value
	 *
	 * @param e shindig value
	 * @return mediated value or null
	 */
	public MEDIATED		toMediated(Enum<SHINDIG> e)
	{
		return (e != null) ? fMapper.from(e.getValue()) : null;
	}

	/**
	 * Given a list of mediated values, return the corresponding shindig values
	 *
	 * @param fromList list of mediatd values
	 * @return list of shindig values
	 */
	public List<Enum<SHINDIG>> toShindigList(List<MEDIATED> fromList)
	{
		List<Enum<SHINDIG>>		toList = new ArrayList<Enum<SHINDIG>>(fromList.size());
		for ( MEDIATED m : fromList )
		{
			toList.add(toShindig(m));
		}

		return toList;
	}

	/**
	 * Given a list of shindig values, return the corresponding mediated values
	 *
	 * @param fromList list of shindig values
	 * @return list of mediated values
	 */
	public List<MEDIATED>		toMediatedList(List<Enum<SHINDIG>> fromList)
	{
		List<MEDIATED>		toList = new ArrayList<MEDIATED>(fromList.size());
		for ( Enum<SHINDIG> e : fromList )
		{
			toList.add(toMediated(e));
		}

		return toList;
	}

	private final EnumMapper<MEDIATED, SHINDIG>		fMapper;
}